package com.vanzo.some.interphone;

import android.util.Log;
import android.util.Xml;

import com.vanzo.some.action.HandAction;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2017/12/28.
 */

public class ConnectParser implements Parse {
    private static final String CONNECTS = "connects";
    private static final String CONNECT = "connect";
    private static final String TITLE = "title";
    private static final String TX = "tx";
    private static final String RX = "rx";
    private static final String COLOR_CODE = "color_code";
    private static final String OWNER_ID = "owner_id";
    private static final String GROUP_ID = "group_id";


    @Override
    public List<Connect> parse(XmlPullParser parser) throws Exception {
        List<Connect> connects = null;
        Connect connect = null;
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    connects = new ArrayList<>();
                    break;
                case XmlPullParser.START_TAG:
                    Log.d("START_TAG", "name = " + parser.getName());
//                    Log.d("VALUE", "value = " + parser.nextText());
                    switch (parser.getName()) {
                        case CONNECT:
                            connect = new Connect();
                            break;
                        case TITLE:
                            connect.setTitle(parser.nextText());
                            break;
                        case TX:
                            connect.setTx(Double.parseDouble(parser.nextText()));
                            break;
                        case RX:
                            connect.setRx(Double.parseDouble(parser.nextText()));
                            break;
                        case COLOR_CODE:
                            connect.setColor_code(Integer.parseInt(parser.nextText()));
                            break;
                        case OWNER_ID:
                            connect.setOwner_id(Integer.parseInt(parser.nextText()));
                            break;
                        case GROUP_ID:
                            connect.setGroup_id(Integer.parseInt(parser.nextText()));
                            break;
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals(CONNECT)) {
                        connects.add(connect);
                        connect = null;
                    }
                    break;
            }
            eventType = parser.next();
        }
        return connects;
    }

    @Override
    public String serialize(List<Connect> connects) throws Exception {
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", false);
        serializer.startTag("", CONNECTS);
        for (Connect connect : connects) {
            serializer.startTag("", CONNECT);

            serializer.startTag("", TITLE);
            serializer.text(connect.getTitle() + "");
            serializer.endTag("", TITLE);

            serializer.startTag("", TX);
            serializer.text(connect.getTx() + "");
            serializer.endTag("", TX);

            serializer.startTag("", RX);
            serializer.text(connect.getRx() + "");
            serializer.endTag("", RX);

            serializer.startTag("", COLOR_CODE);
            serializer.text(connect.getColor_code() + "");
            serializer.endTag("", COLOR_CODE);

            serializer.startTag("", OWNER_ID);
            serializer.text(connect.getOwner_id() + "");
            serializer.endTag("", OWNER_ID);

            serializer.startTag("", GROUP_ID);
            serializer.text(connect.getGroup_id() + "");
            serializer.endTag("", GROUP_ID);

            serializer.endTag("", CONNECT);
        }
        serializer.endTag("", CONNECTS);
        serializer.endDocument();
        return writer.toString();
    }

}
